<?php

class CmpPunishController extends BaseController
{

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate($id)
	{
		
		$model=new CmpPunish;
		$company = Company::model()->findByPk($id);
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
		$model->zm_id = $id;
		if(isset($_POST['CmpPunish']))
		{
			$model->attributes=$_POST['CmpPunish'];
			if($model->save()){
				$company->zc_integral = $company->zc_integral-$model->zcu_integral;
				$company->save();
				//成功跳转
				$this->redirect(array('admin'));
			}
		}

		$this->render('create',array(
			'model'=>$model,
			'company'=>$company,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['CmpPunish']))
		{
			$model->attributes=$_POST['CmpPunish'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->zcu_id));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 单条，批量删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete()
	{
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zcu_id in ($id)");
		//修改内容
		$set = array('zcu_del'=>'2');
		//执行操作
		if(CmpPunish::model()->updateAll($set,$criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}



	/**
	 * 违规管理
	 */
	public function actionAdmin()
	{
		$model=new CmpPunish('search');
		$model->unsetAttributes();  // clear any default values
		$criteria->zcu_del=1;//未删除的产品
		if(isset($_GET['CmpPunish']))
			$model->attributes=$_GET['CmpPunish'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	
	/**
	 * 违规设置
	 */
	public function actionPunishSet(){
		//保存违规原因
		if(isset($_POST['Reason'])){
			$punish = Setting::model()->findByAttributes(array('zkl_variable'=>'PunishReacon'));
			if(!$punish){
				$punish = new Setting();
				$punish->zkl_type = 0;
				$punish->zkl_title = '违规类型';
				$punish->zkl_variable = 'PunishReacon';
			}
			$punish->zkl_valued = serialize($_POST['Reason']);
			if(!$punish->save()){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，违规类型设置失败，请稍后再试！'));
				exit;
			}
		}
		//保存违规总积分
		if(isset($_POST['Sumpoint'])){
			$punish = Setting::model()->findByAttributes(array('zkl_variable'=>'PunishSumpoint'));
			if(!$punish){
				$punish = new Setting();
				$punish->zkl_type = 0;
				$punish->zkl_title = '违规总积分';
				$punish->zkl_variable = 'PunishSumpoint';
			}
			$punish->zkl_valued = $_POST['Sumpoint'];
			if(!$punish->save()){
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，违规类型设置失败，请稍后再试！'));
				exit;
			}
		}
		//保存违规处理
		if(isset($_POST['Result'])){
			$punish = Setting::model()->findByAttributes(array('zkl_variable'=>'PunishResult'));
			if(!$punish){
				$punish = new Setting();
				$punish->zkl_type = 0;
				$punish->zkl_title = '违规处理';
				$punish->zkl_variable = 'PunishResult';
			}
			$punish->zkl_valued = serialize($_POST['Result']);
			if(!$punish->save()){
				//失败跳转
				$this->redirect(Yii::app()->request->urlReferrer);
			}
		}
		$this->render('punish_set');
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=CmpPunish::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='cmp-punish-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
